﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class kecheng_kecheng_check : System.Web.UI.Page
{
    static string forum_id = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        forum_id = Request.Params["forum_id"].ToString();
        Conn conn = new Conn();
        string sql = "select xuesheng from kecheng where topic='" + forum_id + "' and xuesheng like '%/" + Session["sUsername"].ToString() + "/%'";
        string student = conn.sqlFirst(sql);
        if (student != "0")
        {
            //报名时，已经报名的课程不能重复计费！已经报名的课程，转入课程对应的论坛
            Response.Redirect("../bbs/content.aspx?forum_id=" + forum_id);
        }
        else
        {
            //扣除课费时储值金额小于课费时的处理：拒绝或者储值金额为负数。提示金额不足，可以返回索引页
            string chuzhi = conn.sqlFirst("select chuzhi from account where username='" + Session["sUserName"].ToString() + "'");
            double chuzhi_d = double.Parse(chuzhi);
            string xuefei = conn.sqlFirst("select xuefei from kecheng where topic='"+forum_id+"'");
            double xuefei_d = double.Parse(xuefei);
            if (xuefei_d > chuzhi_d)
            {
                Label1.Text = "储值不足以选择本课程，" + " <a href='../kecheng_confirm.aspx'>返回</a>";
            }
            else
            {
                //储值足够选课，给出提示选课，并跳转到论坛页面，或者返回到索引页。
                btnXuanke.Visible = true;
                Label1.Text = " 不选此课程。，" + "<a href='../kecheng_confirm.aspx'>返回</a>";
            }
        }
        conn.Dispose();
    }
    protected void btnXuanke_Click(object sender, EventArgs e)
    {
        //选课，扣除此课程的金额
        Conn conn = new Conn();
        string sql = "select xuefei from kecheng where topic='" + forum_id + "'";
        string xuefei = conn.sqlFirst(sql);
        double xuefei_d = double.Parse(xuefei);
        string chuzhi = conn.sqlFirst("select chuzhi from account where username='" + Session["sUserName"].ToString() + "'");
        double chuzhi_d = double.Parse(chuzhi);
        double shengyuchuzhi = chuzhi_d - xuefei_d;
        sql = "update account set chuzhi =" + shengyuchuzhi.ToString() + " where username='" + Session["sUserName"].ToString() + "'";
        if (conn.ExeSql(sql) == 1)
        {
            //修改课程表xuesheng字段，加入此学生
            sql = "update kecheng set xuesheng='/" + Session["sUserName"].ToString() + "/'";
            if (conn.ExeSql(sql) == 1)
            {
                Response.Redirect("../bbs/content.aspx?forum_id=" + forum_id);
            }
            else
            {
                Label1.Text = "选课操作2没有成功，" + " <a href='../kecheng_confirm.aspx'>返回</a>";
            }
        }
        else
        {
            Label1.Text = "选课操作没有成功，" + " <a href='../kecheng_confirm.aspx'>返回</a>";
        }
    }
}
